/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.ParameterBean;
import com.maywide.jdp.system.entities.Organization;
import com.maywide.jdp.system.entities.Parameter;
import com.maywide.jdp.system.utils.JSONEditor;

/**
 * 参数管理Service
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("parameterService")
public class ParameterServiceImpl implements ParameterService {
	/**
	 * 参数管理Bean接口
	 */
	private ParameterBean bean = null;

	/**
	 * @param bean
	 *            the bean to set
	 */
	@Autowired
	@Qualifier("parameterBean")
	public void setBean(ParameterBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping("/system/parameter/insert.json")
	@Override
	public Parameter insert(
			@RequestParam(value = "parameter") Parameter parameter,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.insert(parameter);
		return parameter;
	}

	@JSON
	@RequestMapping("/system/parameter/update.json")
	@Override
	public Parameter update(
			@RequestParam(value = "parameter") Parameter parameterVO,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		String id = parameterVO.getId();
		Parameter parameter = (Parameter) bean.load(id);

		// 取得不可改变属性
		Organization entity = parameter.getEntity();
		parameterVO.setEntity(entity);
		BeanUtils.copyProperties(parameterVO, parameter);

		bean.update(parameter);
		return parameter;
	}

	@JSON
	@RequestMapping("/system/parameter/load.json")
	@Override
	public Parameter load(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Parameter parameter = (Parameter) bean.load(id);
		return parameter;
	}

	@RequestMapping("/system/parameter/lock.json")
	@Override
	public void lock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping("/system/parameter/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.unlock(id);
	}

	@RequestMapping("/system/parameter/move.json")
	@Override
	public void move(@RequestParam(value = "ids") List<String> ids,
			@RequestParam(value = "oldID") String oldOrgID,
			@RequestParam(value = "targetID") String newOrgID,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.move(ids, oldOrgID, newOrgID);
		if (oldOrgID == null || oldOrgID.equals("")
				|| oldOrgID.equals("root"))
			oldOrgID = "system";
		if (newOrgID == null || newOrgID.equals("")
				|| newOrgID.equals("root"))
			newOrgID = "system";
	}

	@RequestMapping("/system/parameter/remove.json")
	@Override
	public void remove(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.remove(Parameter.class, id);
	}

	@JSON
	@RequestMapping("/system/parameter/page.json")
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "entityID", required = false) String entityID,
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "active", required = false) String active,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();

		if (key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}

		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if (entityID != null && !entityID.equals("param_org_root"))
			conditions.put("entityID", entityID);
		else
			conditions.put("noEntityID", null);

		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Parameter.class, new JSONEditor(
				Parameter.class));
	}
}
